<?php

namespace Api\Controller;

class IndexController extends ApiController{
    
    //首页
    public function index(){
        
        $return  = null;
        //获取幻灯片广告
        $slider = M('Slider')->where(['code'=>'app_index'])->find();
        if(!empty($slider)){
            $where_s = array();
            $where_s['status'] = 1;
            $where_s['slider_id'] = $slider['id'];
            $where_s['start_time'] = array('lt',time());
            $where_s['end_time'] = array('gt', time());

            $slider['items'] = M('SliderItem')
                ->where($where_s)
                ->order('sort asc')->select();
        }
        //banner
        $return['sliders'] = $slider;
        //获取最后一张
        $return['last_slider'] = $return['sliders']['items'][count($return['sliders']['items'])-1];
        
        //文章分类
        $article_cats = S('ArticleCategory');
        if(!$article_cats){
            $article_cats = A('Article')->cache();
        }
        
        //名家论市
        $Blog = D('Wap/Blog'); 
        $l_blog = M('Blog')->where(array('uid'=>1001))->find(); 
        $return['blog_laoguai'] = $Blog->getArticleByUser(array('userid'=>1001),2)['data'];
        $return['mingjia'] = $Blog->getArticles(array('blog_id'=>array('neq',$l_blog['id'])),4,0,'is_top desc,id desc')['data'];

        foreach($return['blog_laoguai'] as $k=>$v){
            $return['blog_laoguai'][$k]['createTime'] = date('Y-m-d', $v['create_time']);
        }
        foreach($return['mingjia'] as $k=>$v){
            $return['mingjia'][$k]['createTime'] = date('Y-m-d', $v['create_time']);
        }
        //需要显示在导航的分类
        $return['show_cats'] = [];
        $show_c_ids = [4,23,31];
        $sort = [];
        foreach ($article_cats as $k=>$v){
            if(in_array($v['id'],$show_c_ids)){
                $return['show_cats'][] = $v;
                $sort[] = $v['sort'];
            }
        }
//        array_multisort($sort, SORT_ASC, $return['show_cats']);

        //最新资讯
        $articles = M('Article')->field('id,title,source,thumb,catid,publish_time,top')->where(['catid'=>['neq',3]])->order('top desc,id desc')->limit(20)->select();
        foreach ($articles as $k=>$v){
            if(isset($article_cats[$v['catid']])){
                $articles[$k]['catname'] = $article_cats[$v['catid']]['name'];
            }
            $articles[$k]['publishTime'] = date('Y-m-d', $v['publish_time']);
        }
        $return['articles'] = $articles;
        
        if(!empty($return)){
            $this->success($return, 'Ok');
        }
        
        $this->error('没有获取到数据');
    }

    //发送邮件
    public function send_email(){
        $email = I('email');
        if(!isEmail($email)){
            $this->error('邮箱格式错误');
        }
        $user = M('User')->where(array('email'=>$email))->count();
        if(empty($user)){
            $this->error('该用户不存在');
        }

        $code = mt_rand(111111, 999999);
        session(array('name'=>'forget_email','expire'=>600));
        session('forget_email', $code);
        $content = '用户找回密码，验证码'.$code.'，有效期10分钟';

        if($this->send_mail($email, $content)){
            $this->success([],'邮件已发送');
        }

        $this->error('邮件发送失败');
    }
}
